import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import lunbotu from "@/views/lunbotu";

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: '导航栏',
      component: () => import('@/views/dashboard/index'),
      meta: {title: '导航栏', icon: 'dashboard'}
    }]
  },

  {
    path: '/Goods',
    component: Layout,
    name: 'Goods',
    redirect: '/Goods/query',
    meta: {title: '商品类', icon: 'el-icon-s-help'},
    children: [
      {
        path: 'query',
        name: '查询商品',
        component: () => import('@/views/Goods/Query'),
        meta: {title: '查询商品', icon: 'table'}
      },
      {
        path: 'historyPrice',
        name: '历史价格',
        component: () => import('@/views/Goods/HistoryPrice'),
        meta: {title: '历史价格', icon: 'tree'}
      },
      {
        path: 'ckquery',
        name: '查询仓库商品',
        component: () => import('@/views/Goods/CkQuery'),
        meta: {title: '查询仓库商品', icon: 'tree'}
      },
      {
        path: 'series',
        name: '商品系列查询',
        component: () => import('@/views/Goods/seriesQuery'),
        meta: {title: '商品系列查询', icon: 'tree'}
      }
    ]
  },
  {
    path: '/custom',
    component: Layout,
    name: '客户类',
    meta: {title: '客户类', icon: 'el-icon-s-help'},
    children: [
      {
        path: 'index',
        name: '客户类',
        component: () => import('@/views/Custom'),
        meta: {title: '查询客户', icon: 'form'}
      },
      {
        path: 'exportList',
        name: '导出对账单',
        component: () => import('@/views/Custom/exportList'),
        meta: {title: '导出对账单', icon: 'form'}
      }
    ]
  },
  {
    path: '/zd',
    component: Layout,
    redirect: '/zd/query',
    name: '账单类',
    meta: {title: '账单类', icon: 'el-icon-s-help'},
    children: [
      {
        path: 'query',
        name: '查询出货账单',
        component: () => import('@/views/ZD/query'),
        meta: {title: '查询出货账单', icon: 'form'}
      },
      {
        path: 'jhquery',
        name: '查询进货账单',
        component: () => import('@/views/ZD/JHquery'),
        meta: {title: '查询进货账单', icon: 'form'}
      },
      {
        path: 'ckQuery',
        name: '仓库出货单',
        component: () => import('@/views/ZD/CKSale'),
        meta: {title: '仓库出货单', icon: 'form'}
      },
      {
        path: 'ckPur',
        name: '仓库入仓单',
        component: () => import('@/views/ZD/CKPur'),
        meta: {title: '仓库入仓单', icon: 'form'}
      },
      {
        path: 'queryOrder',
        name: '查询出货订单账单',
        component: () => import('@/views/ZD/order'),
        meta: {title: '查询出货订单账单', icon: 'form', keepAlive: true}
      },
      {
        path: 'detail',
        name: '账单详情',
        component: () => import('@/views/ZD/detail'),
        meta: {title: '账单详情', icon: 'form'}
      },
      {
        path: 'statistic',
        name: '重量统计查询',
        component: () => import('@/views/statistic/index.vue'),
        meta: {title: '重量统计查询', icon: 'form'}
      }
    ]
  },
  {
    path: '/advance',
    component: Layout,
    redirect: '/advance/index',
    name: '高级查询',
    meta: { title: '高级查询', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'index',
        name: '高级查询',
        component: () => import('@/views/advance/index'),
        meta: {title: '高级查询', icon: 'dashboard'}
      }
    ]
  },
  {
    path: '/hr',
    component: Layout,
    children: [
      {
        path: 'index',
        name: '人事类',
        component: () => import('@/views/hr/sign'),
        meta: {title: '考勤情况', icon: 'form'}
      }
    ]
  },
  {
    path: '/assistant',
    component: Layout,
    meta: {title: '助手', icon: 'el-icon-s-help'},
    children: [
      {
        path: 'index',
        name: '助手',
        component: () => import('@/views/assistant/assistant'),
        meta: {title: '助手', icon: 'form'}
      },
      {
        path: 'log',
        name: '日志',
        component: () => import('@/views/assistant/operateLog'),
        meta: {title: '日志', icon: 'form'}
      },
      {
        path: 'logger',
        name: '日志',
        component: () => import('@/views/assistant/logger'),
        meta: {title: '新日志', icon: 'form'}
      }
    ]
  },
  {
    path: '/setting',
    component: Layout,
    meta: {title: '设置', icon: 'setting'},
    children: [
      {
        path: 'wecomNotice',
        name: '企业微信通知设置',
        component: () => import('@/views/setting/setting'),
        meta: {title: '企微设置', icon: '企微'}
      }
    ]
  },
  {
    path: '/alert',
    component: Layout,
    meta: {title: '库存报警', icon: 'el-icon-s-help'},
    children: [
      {
        path: '',
        name: '库存报警',
        component: () => import('@/views/statistic/alert'),
        meta: {title: '库存报警', icon: 'form'}
      }
    ]
  },
  // 404 page must be placed at the end !!!
  {path: '*', redirect: '/404', hidden: true}
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({y: 0}),
  routes: constantRoutes
})

const router = createRouter()

router.beforeEach((to, from, next) => {
  const isLogin = localStorage.getItem("isLogin")
  if (to.name !== 'Login' && isLogin !== 'true') {
    next({path: '/login'})
  } else if (to.name === 'Login' && isLogin === 'true') {
    next(false)
  } else {
    next()
  }
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
